package cn.credit.loan.controller;

import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
/**
 * 合同控制器类
 * @author
 *
 */
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.credit.loan.entity.Contract;
import cn.credit.loan.service.contract.ContractService;
/**
 * 合同控制类
 * @author 
 *
 */
@Controller
public class ContractController {
	//日志输出
	private Logger logger = Logger.getLogger(ContractController.class);
	//注入
	@Autowired
	private ContractService contractService;
	/**
	 * 合同添加
	 * @param contract
	 * @param model
	 * @return
	 */
	@RequestMapping("/addcontract")
	public String addContract(Contract contract,ModelMap model)
	{
		try {
			contract.setContContent("今天。。。。。。。。。。");
			contract.setLoanId("3");
			contract.setManagerId("3");
			contract.setContCreateDate(new Date());
			contract.setContSignDate(new Date());
			boolean contracts=contractService.contractadd(contract);
			if(contracts){
			model.addAttribute("contract", contract);
			return "Contractadd";
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
		
	}
	
	/**
	 * 合同更新
	 * @param contract
	 * @param model
	 * @return
	 */
	@RequestMapping("/updatecontract")
	public String updateContract(Contract contract,ModelMap model)
	{
		try {
			
			contract.setContName("测试005");
			contract.setContContent("今天。。。。。。。。。。");
			contract.setLoanId("5");
			contract.setManagerId("5");
			contract.setContCreateDate(new Date());
			contract.setContSignDate(new Date());
			boolean contracts=contractService.contractupdate(contract);
			if(contracts){
			model.addAttribute("contract", contract);
			return "Contractupdate";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * 合同删除
	 * @param contId
	 * @param model
	 * @return
	 */
	@RequestMapping("/deletecontract")
	public String deletecontract(int contId,ModelMap model)
	{
		try {
			boolean contracts=contractService.contractdelete(contId);
			if(contracts)
			{
				model.addAttribute("contId",contId);
				return "Contractdelte";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
		
	}
	
	/**
	 * 合同查询
	 * @param contract
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/listcontract")
	@ResponseBody
	public Object ListContract(Contract contract,ModelMap model){
		try {
			List<Contract> contracts= contractService.getContractList(contract);
			if(contract!=null)
			{
				model.addAttribute("contract", contract);
				return contracts;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
		
	}
}
